package com.changpay.zywy.ui.activity.count;

import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ruffian.library.widget.RTextView;
import com.changpay.zywy.R;
import com.changpay.zywy.ui.dialog.SelectMonthDialog;
import com.changpay.zywy.utils.DateUtil;
import com.changpay.zywy.utils.DialogUtil;
import com.changpay.zywy.wight.WheelCircleView;
import kale.ui.view.dialog.EasyDialog;

import java.util.Calendar;

/**
 * Created by AndroidIntexh1 on 2018/12/20.
 */

public class CountAddShopView extends RelativeLayout {

    private int date_type_index = 0;
    private final int default_day = 0;
    private final int default_week = 1;
    private final int default_month = 2;
    private final int search_day = 3;
    private final int search_week = 4;
    private final int search_month = 5;

    RelativeLayout rvMore;
    TextView tvTitle;
    LinearLayout llContent;


    RTextView tvTimeSearch;
    TextView tv_shop_add_count;  // 新增商户数
    TextView tv_shop_total_count; // 总商户数
    TextView tv_show_time; // 用于选择展示的时间

    TextView tv_add_name_one; // 总商户数
    TextView tv_add_name_two; // 用于选择展示的时间

    public RadioGroup count_add_shop_rg; // 年月日选择

    WheelCircleView ct_add_shop_wheel;  // 比例图

    FragmentManager fragmentManager;
    CountChartAddShopClose countChartOtherClose;
    public boolean isDisplay = false;


    private View view;
    private DatePickerDialog.OnDateSetListener onWeekDateSetListener;
    private DatePickerDialog.OnDateSetListener onDayDateSetListener;
    private String day;


    public CountAddShopView(Context context) {
        this(context, null);
    }

    public CountAddShopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        day = DateUtil.INSTANCE.stampToDate("yyyy-MM-dd", System.currentTimeMillis());
        view = View.inflate(context, R.layout.count_add_shop_view, this);
        tvTitle = view.findViewById(R.id.tv_title);
        llContent = view.findViewById(R.id.llContent);
        tvTimeSearch = view.findViewById(R.id.tv_time_search);
        count_add_shop_rg = view.findViewById(R.id.count_add_shop_rg);

        tv_shop_add_count = view.findViewById(R.id.tv_shop_add_count);

        tv_shop_total_count = view.findViewById(R.id.tv_shop_total_count);
        tv_add_name_one = view.findViewById(R.id.tv_add_name_one);

        tv_add_name_two = view.findViewById(R.id.tv_add_name_two);



        ct_add_shop_wheel = view.findViewById(R.id.ct_add_shop_wheel);
        tv_show_time = view.findViewById(R.id.tv_show_time);

        count_add_shop_rg.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.day_rb:
                    tv_show_time.setText(day);
                    initDateData(default_day, day + "|" + day);
                    break;
                case R.id.week_rb:
                    String NowDate = DateUtil.INSTANCE.getPastDate(0);
                    String pastDate = DateUtil.INSTANCE.getPastDate(6);
                    tv_show_time.setText(pastDate + " 至 " + NowDate);
                    initDateData(default_week, pastDate + "|" + NowDate);
                    break;
                case R.id.month_rb:
                    tv_show_time.setText(day);
                    Calendar instance = Calendar.getInstance();
                    String firstDayOfMonth1 =
                            DateUtil.INSTANCE.getFirstDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH));
                    String lastDayOfMonth1 =
                            DateUtil.INSTANCE.getLastDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH));
                    String thisMonth = DateUtil.INSTANCE.getYearMonth(System.currentTimeMillis());
                    tv_show_time.setText(thisMonth.substring(0, 4) + "-" + thisMonth.substring(4, 6) );
                    initDateData(default_month, firstDayOfMonth1 + "|" + lastDayOfMonth1);
                    break;
            }
        });


        tvTimeSearch.setOnClickListener(view -> {
            switch (date_type_index) {
                case default_day:
                case search_day:
                    DialogUtil.INSTANCE.showDatePickerDialog(context, onDayDateSetListener, Calendar.getInstance());
                    break;
                case default_week:
                case search_week:
                    DialogUtil.INSTANCE.showDatePickerDialog(context, onWeekDateSetListener, Calendar.getInstance());
                    break;
                case default_month:
                case search_month:
                    SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(context);
                    builder.setOnDialogClickListener((v, month) -> {
                        String date = month.substring(0, 4) + "-" + month.substring(4, 6);
                        tv_show_time.setText(month.substring(0, 4) + "年" + month.substring(4, 6) + "月");
                        String date_one = DateUtil.INSTANCE.getFirstDayOfMonth1(Integer.valueOf(month.substring(0, 4)), Integer.valueOf(month.substring(4, 6)) - 1);
                        String date_two = DateUtil.INSTANCE.getLastDayOfMonth1(Integer.valueOf(month.substring(0, 4)), Integer.valueOf(month.substring(4, 6)) - 1);
                        Log.e("mango", "onDateSet month: " + date_one + "|" + date_two);
                        initDateData(date_type_index, date_one + "|" + date_two);
                    });
                    EasyDialog build = builder.build();
                    build.setCancelable(true);
                    build.show(fragmentManager);
                    break;
            }
        });

        onDayDateSetListener = (datePicker, year, month, day) -> {
            String select_date = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", day);
            Log.e("mango", "onDateSet day: " + select_date);
            String date = select_date + "|" + select_date;
            tv_show_time.setText(select_date);
            initDateData(search_day, date);
        };
        onWeekDateSetListener = (datePicker, year, month, day) -> {
            String select_date = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", day);
            String start_date = DateUtil.INSTANCE.getSpecifiedDayBefore(select_date, 6);
            String date = start_date + "|" + select_date;
            Log.e("mango", "onDateSet week : " + date);
            tv_show_time.setText(start_date + "至" + select_date);
            initDateData(search_week, date);
        };
    }

    private void initDateData(int type, String date) {
        date_type_index = type;
        Log.e("mango", "initDateData  : " + date);
        countChartOtherClose.loadActionMonthCharData(date_type_index, date);
    }

    public void setData(int dataOne, int dataTwo) {
        tv_shop_add_count.setText(String.valueOf(dataOne) + "个");
        tv_shop_total_count.setText(String.valueOf(dataTwo) + "个");
        initPerChart(ct_add_shop_wheel, dataOne, dataTwo);
    }

    private void initPerChart(WheelCircleView chart, int dataOne, int dataTwo) {
        chart.setDataPer(dataOne, dataTwo);
    }


    public void setListener(CountChartAddShopClose listener) {
        countChartOtherClose = listener;
    }

    public void setFragmentManager(FragmentManager manager) {
        fragmentManager = manager;
    }

    public void setTitle(String title,String name_one,String name_two) {
        if (tvTitle != null) tvTitle.setText(title);
        tv_add_name_one.setText(name_one);
        tv_add_name_two.setText(name_two);
    }

    public interface CountChartAddShopClose {

        void loadActionMonthCharData(int type, String month);
    }


}
